package com.ecube.test.control;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.ExchangeFunctionEnum;
import com.ecube.protocol.exchange.control.ExchangePowerAdjustSetDischargeResp;
import com.ecube.protocol.exchange.control.ExchangePowerAdjustSetResp;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

/**
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/4 11:30
 */
public class SendExchangeControl {

    public static void main(String[] args) {
        //sendAdjustSetResp("FAT", "100030001","126725", ChargerInfo.builder().chgId("0817417302790889").gunNo(1).index(101).build());
        //sendAdjustSetResp("FAT", "100030001","126725", ChargerInfo.builder().chgId("0817062302793875").gunNo(1).index(102).build());
        sendAdjustSetResp("FAT", "100030001","400411", ChargerInfo.builder().chgId("4004111234567892").gunNo(1).index(514).build());

        //sendAdjustSetDischargeResp("FAT", "100030001","126725", ChargerInfo.builder().chgId("0817417302790889").gunNo(2).index(41).build());
        //sendAdjustSetDischargeResp("FAT", "100030001","400411", ChargerInfo.builder().chgId("4004111234567892").gunNo(1).index(262).build());
    }

    /**
     * 充电设置-回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    private static void sendAdjustSetResp(String env, String productCode, String deviceCode, ChargerInfo chargerInfo) {
        ExchangePowerAdjustSetResp adjustSetResp = new ExchangePowerAdjustSetResp();
        ExchangeFunctionEnum function = (ExchangeFunctionEnum)EnumUtils.getFunction(adjustSetResp.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V3.1.1");
        reportHand.setIndex(chargerInfo.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置充电机信息
        adjustSetResp.setChgID(chargerInfo.getChgId());
        adjustSetResp.setGunNo(chargerInfo.gunNo);
        reportStandard.setDataBody(adjustSetResp);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    /**
     * 放电设置-回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    private static void sendAdjustSetDischargeResp(String env, String productCode, String deviceCode, ChargerInfo chargerInfo) {
        ExchangePowerAdjustSetDischargeResp adjustSetDischargeResp = new ExchangePowerAdjustSetDischargeResp();
        ExchangeFunctionEnum function = (ExchangeFunctionEnum)EnumUtils.getFunction(adjustSetDischargeResp.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V3.1.1");
        reportHand.setIndex(chargerInfo.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置充电机信息
        adjustSetDischargeResp.setChgID(chargerInfo.getChgId());
        adjustSetDischargeResp.setGunNo(chargerInfo.gunNo);
        reportStandard.setDataBody(adjustSetDischargeResp);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    @Data
    @Builder
    private static class ChargerInfo {

        /**
         * 充电机编号
         */
        private String chgId;

        /**
         * 充电枪
         */
        private Integer gunNo;

        /**
         * 序号
         */
        private Integer index;
    }
}
